<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.model.pojo.*,com.tianxiabuyi.utils.*"%>
<%@ page import="org.jsoup.Jsoup,org.jsoup.nodes.Element,org.jsoup.select.Elements"%>
<%@ page import="org.dom4j.*"%>
<%@ page import="com.alibaba.fastjson.TypeReference" %>
<%@ page import="com.tianxiabuyi.persist.QBuilder" %>
<%!
    // 网络抓取方法
    // 替换抓取新闻标题中可能出现的特殊字符
  	public String replaceTitle(String title) {
  		title = Tools.getDesc(title, "");
  		title = title.replaceAll("\"", "&quot;").replaceAll(":", "&#58;");
  		return title;
  	}

  	// 替换新闻内容中的html标签
  	public String replaceContent(String html) {
  		html = Tools.getDesc(html, "");
  		html = html.toLowerCase();
  		html = html.replaceAll("<p[^>]*?>", "<p>");
  		html = html.replaceAll("<div[^>]*?>", "<p>");
  		html = html.replaceAll("</div>", "</p>");
  		html = html.replaceAll("<font[^>]*?>", "");
  		html = html.replaceAll("<b[^>]*?>", "");
  		html = html.replaceAll("<span[^>]*?>","");
  		html = html.replaceAll("</span>","");
  		html = html.replaceAll("</font>","");
  		html = html.replaceAll("<br[^>]*?>","<br/>");
  		html = html.replaceAll("<style[^>]*?>[\\s\\S]*?<\\/style>","");
  		html = html.replaceAll("<a[^>]*?>","");
  		html = html.replaceAll("</a>", "");
  		html = replaceTitle(html);
  		int i = 0;
  		while(html.indexOf("<img") > -1) {
  			html = html.replaceFirst("<img[^>]*?>", "!--IMG#" + (i++)  + "--");
  		}
  		return html;
  	}

  	// 抓取远程图片到本地服务器，并返回本地服务器地址集合
  	public String imgs(Element content, String domian) {
  		Elements imgs = content.select("img");
  		List<HashMap<String, String>> imgsJson = new ArrayList<HashMap<String, String>>();
  		int size = imgs.size();
  		for(int i = 0; i < size; i++) {
  			String src = imgs.get(i).attr("src").toString();
  			String domianIgnore = domian.replace("www.","");
  			if((!src.startsWith(domian)) && (!src.startsWith(domianIgnore))) {
  				src = domian + src;
  			}

  			String big = ImageUtil.big(src);
  			String sizeJson = ImageUtil.size(big);
  			String pixel  = "550 * 366";
  			List<HashMap<String, String>> sizeList = JSONUtil.fromJson(sizeJson, new TypeReference<List<HashMap<String, String>>>(){});
  			if(sizeList.size() > 0) {
  				HashMap<String, String> sizeMap = sizeList.get(0);
  				pixel = sizeMap.get("width") + " * " + sizeMap.get("height");
  			}

  			// 存入HashMap中
  			HashMap<String, String> map = new HashMap<String, String>();
  			map.put("ref","!--IMG#" + i + "--");
  			map.put("pixel", pixel);
  			map.put("src", big);
  			// 循环添加某篇新闻
  			imgsJson.add(map);
  		}
  		return JSONUtil.toJson(imgsJson);
  	}

  	// 如果医院新闻id不为数字，使用本地服务器流水号
  	public void genNewsId(List<Integer> ids, int hospital) {
  		StringBuffer buffer = new StringBuffer();
  		for (int i = 0; i < ids.size(); i++) {
  			buffer.append(ids.get(i)).append(",");
  		}
  		if (buffer.length() == 0) return;
  		String allId = buffer.toString();
  		allId = allId.substring(0, allId.length() -1);

  		List<News> list = QBuilder.db("basedb").queryHql("from News where hospital='" + hospital + "' and id in(" + allId + ")");
  		for (int i = 0; i < list.size(); i++) {
  			News news = list.get(i);
  			int id = news.getId();
  			news.setNewsId(news.getId());
  		}
  		DBTools.db("basedb").modItem(list);
  	}

  	// 替换图片
  	public Map replaceImage(String html, String domian) {
  		if (html == null) return new HashMap();
  		html = html.replaceAll("\"", "");
  		org.jsoup.nodes.Document doc = Jsoup.parse(html);
  		Elements imgs = doc.select("img");
  		List<Map<String, String>> imgList = new ArrayList<Map<String, String>>();
  		for (int i = 0; i < imgs.size(); i++) {
  			Element img = imgs.get(i);
  			String src = img.attr("src").toString();
  			String domianIgnore = domian.replace("www.","");
  			if((!src.startsWith(domian)) && (!src.startsWith(domianIgnore))) {
  				src = domian + src;
  			}
  			String big = ImageUtil.big(src);
  			Map<String, String> imgMap = new HashMap<String, String>();
  			imgMap.put("src", big);
  			imgList.add(imgMap);

  			html = html.replace(img.attr("src").toString(), big);
  		}
  		Map returnMap = new HashMap();
  		returnMap.put("html", html);
  		returnMap.put("imgs", imgList);
  		return returnMap;
  	}
%>
